﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace YBOT_Field_Control
{
    class LogWriter
    {
        /// <summary>
        /// Path used for YBOT feild control files
        /// </summary>
        /// <returns>Path as a string (File located on user's desktop</returns>
        public string YBOTFilePath()
        {
            //Store date as a string
            string date = DateTime.Today.ToLongDateString();

            //Directory path for YBOT 1 on the user's desktop
            string YBOTpath = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Desktop\YBOT Field Control Program Files");

            //Path for todays date
            string path = YBOTpath + @"\" + date;

            //Check to see if folder exists if not then make it; fail no path is created
            try
            {
                if (!Directory.Exists(YBOTpath))
                {
                    Directory.CreateDirectory(YBOTpath);
                    Directory.CreateDirectory(path);
                }
                else if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
            catch
            {
                path = null;
            }

            //Return path name
            return path;
        }

        public void WriteLog(string text)
        {
            string path = (YBOTFilePath() + @"\Logs");          //Log Directory
            string file = "\\YBOT_Field_Control_Log.txt";        //Log file name

            //Try and write data to file, return if fail
            try
            {
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string date = DateTime.Today.ToShortDateString();
                string time = DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond.ToString(); 
                text = (date + "_" + time + " :: " + text);
                FileStream fs = new FileStream((path + file), FileMode.Append, FileAccess.Write, FileShare.Write);
                fs.Close();
                StreamWriter sw = new StreamWriter((path + file), true, Encoding.ASCII);
                sw.WriteLine(text);
                sw.Close();
            }
            catch
            {
                return;
            }

        }

        public void WriteLog(string text, string fileName)
        {
            string path = (YBOTFilePath() + @"\Logs");  //Log Directory
            string file = "\\"+ fileName + ".txt";        //Log file name

            //Try and write data to file, return if fail
            try
            {
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string date = DateTime.Today.ToShortDateString();
                string time = DateTime.Now.ToLongTimeString() + "_" + DateTime.Now.Millisecond.ToString();
                text = (date + "_" + time + " :: " + text);
                FileStream fs = new FileStream((path + file), FileMode.Append, FileAccess.Write, FileShare.Write);
                fs.Close();
                StreamWriter sw = new StreamWriter((path + file), true, Encoding.ASCII);
                sw.WriteLine(text);
                sw.Close();
            }
            catch
            {
                return;
            }

        }

        public void WriteLog(string text, bool debug)
        {
            string path = (YBOTFilePath() + @"\Logs");  //Log Directory
            string file = "\\YBOT_Field_Control_Log.txt";        //Log file name

            //If debug then use this file name
            if (debug) file = "\\YBOT_Field_Control_Log-DEBUG.txt";        //Log file name

            //Try and write data to file, return if fail
            try
            {
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string date = DateTime.Today.ToShortDateString();
                string time = DateTime.Now.ToLongTimeString() + "_" + DateTime.Now.Millisecond.ToString();
                text = (date + "_" + time + " :: " + text);
                FileStream fs = new FileStream((path + file), FileMode.Append, FileAccess.Write, FileShare.Write);
                fs.Close();
                StreamWriter sw = new StreamWriter((path + file), true, Encoding.ASCII);
                sw.WriteLine(text);
                sw.Close();
            }
            catch
            {
                return;
            }

        }

        public void WriteLog(string text, string fileName, bool debug)
        {
            string path = (YBOTFilePath() + @"\Logs");  //Log Directory
            string file = "\\" + fileName + ".txt";        //Log file name

            if (debug) file = "\\" + fileName + "-DEBUG.txt";        //Log file name

            //Try and write data to file, return if fail
            try
            {
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string date = DateTime.Today.ToShortDateString();
                string time = DateTime.Now.ToLongTimeString() + "_" + DateTime.Now.Millisecond.ToString();
                text = (date + "_" + time + " :: " + text);
                FileStream fs = new FileStream((path + file), FileMode.Append, FileAccess.Write, FileShare.Write);
                fs.Close();
                StreamWriter sw = new StreamWriter((path + file), true, Encoding.ASCII);
                sw.WriteLine(text);
                sw.Close();
            }
            catch
            {
                return;
            }

        }

        public void WriteLog(string text, string fileName, string _path)
        {
            string path = (YBOTFilePath() + @"\" + _path);  //Log Directory
            string file = "\\" + fileName + ".txt";        //Log file name

            //Try and write data to file, return if fail
            try
            {
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string date = DateTime.Today.ToShortDateString();
                string time = DateTime.Now.ToLongTimeString() + "_" + DateTime.Now.Millisecond.ToString();
                text = (date + "_" + time + " :: " + text);
                FileStream fs = new FileStream((path + file), FileMode.Append, FileAccess.Write, FileShare.Write);
                fs.Close();
                StreamWriter sw = new StreamWriter((path + file), true, Encoding.ASCII);
                sw.WriteLine(text);
                sw.Close();
            }
            catch
            {
                return;
            }

        }

        public void WriteLog(string text, string fileName, string _path, bool debug)
        {
            string path = (YBOTFilePath() + @"\" + _path);  //Log Directory
            string file = "\\" + fileName + ".txt";        //Log file name

            if (debug) file = "\\" + fileName + "-DEBUG.txt";        //Log file name

            //Try and write data to file, return if fail
            try
            {
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string date = DateTime.Today.ToShortDateString();
                string time = DateTime.Now.ToLongTimeString() + "_" + DateTime.Now.Millisecond.ToString();
                text = (date + "_" + time + " :: " + text);
                FileStream fs = new FileStream((path + file), FileMode.Append, FileAccess.Write, FileShare.Write);
                fs.Close();
                StreamWriter sw = new StreamWriter((path + file), true, Encoding.ASCII);
                sw.WriteLine(text);
                sw.Close();
            }
            catch
            {
                return;
            }

        }

    }
}
